﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace LunarSF.SHomeWorkshop.LunarMarkdownEditor
{
    class EditorPopupToolBar : Popup
    {
        public EditorPopupToolBar(MarkdownEditorBase editorBase)
        {
            this.AllowsTransparency = true;
            this.PopupAnimation = PopupAnimation.Scroll;
            this.StaysOpen = false;
            this.Placement = PlacementMode.Mouse;

            this.masterEditorBase = editorBase;

            System.Windows.Style btnStyle = new System.Windows.Style();
            var btnBackgroundTrigger = new System.Windows.Trigger()
            {
                Property = IsMouseOverProperty,
                Value = true,
            };
            btnBackgroundTrigger.Setters.Add(new System.Windows.Setter()
            {
                Property = Border.BackgroundProperty,
                Value = Brushes.LightBlue,
            });
            btnStyle.Triggers.Add(btnBackgroundTrigger);

            var baseGrid = new Grid();
            baseGrid.RowDefinitions.Add(new RowDefinition());
            baseGrid.RowDefinitions.Add(new RowDefinition());

            baseGrid.ColumnDefinitions.Add(new ColumnDefinition());
            baseGrid.ColumnDefinitions.Add(new ColumnDefinition());
            baseGrid.ColumnDefinitions.Add(new ColumnDefinition());
            baseGrid.ColumnDefinitions.Add(new ColumnDefinition());
            baseGrid.ColumnDefinitions.Add(new ColumnDefinition());
            baseGrid.ColumnDefinitions.Add(new ColumnDefinition());

            Border btnCut = new Border()
            {
                Style = btnStyle,
                Margin = new System.Windows.Thickness(1),
                Width = 24,
                Height = 24,
                BorderBrush = Brushes.Black,
                BorderThickness = new System.Windows.Thickness(1),
                ToolTip = "Ctrl+X，剪切文本",
            };
            btnCut.Child = new Image()
            {
                Width = 16,
                Height = 16,
                Source = new BitmapImage(new Uri("pack://application:,,,/LunarMarkdownEditor;component/Images/CutHS.png")),
            };
            btnCut.MouseLeftButtonDown += BtnCut_MouseLeftButtonDown;
            baseGrid.Children.Add(btnCut);
            Grid.SetColumn(btnCut, 0);

            Border btnCopy = new Border()
            {
                Style = btnStyle,
                Margin = new System.Windows.Thickness(1),
                Width = 24,
                Height = 24,
                BorderBrush = Brushes.Black,
                BorderThickness = new System.Windows.Thickness(1),
                ToolTip = "Ctrl+C，复制文本",
            };
            btnCopy.Child = new Image()
            {
                Width = 16,
                Height = 16,
                Source = new BitmapImage(new Uri("pack://application:,,,/LunarMarkdownEditor;component/Images/CopyHS.png")),
            };
            btnCopy.MouseLeftButtonDown += BtnCopy_MouseLeftButtonDown;
            baseGrid.Children.Add(btnCopy);
            Grid.SetColumn(btnCopy, 1);

            Border btnPaste = new Border()
            {
                Style = btnStyle,
                Margin = new System.Windows.Thickness(1),
                Width = 24,
                Height = 24,
                BorderBrush = Brushes.Black,
                BorderThickness = new System.Windows.Thickness(1),
                ToolTip = "Ctrl+V，粘贴文本",
            };
            btnPaste.Child = new Image()
            {
                Width = 16,
                Height = 16,
                Source = new BitmapImage(new Uri("pack://application:,,,/LunarMarkdownEditor;component/Images/PasteHS.png")),
            };
            btnPaste.MouseLeftButtonDown += BtnPaster_MouseLeftButtonDown;
            baseGrid.Children.Add(btnPaste);
            Grid.SetColumn(btnPaste, 2);

            Border btnBold = new Border()
            {
                Style = btnStyle,
                Margin = new System.Windows.Thickness(1),
                Width = 24,
                Height = 24,
                BorderBrush = Brushes.Black,
                BorderThickness = new System.Windows.Thickness(1),
                ToolTip = "Ctrl+B，切换加粗，不支持跨行",
            };
            btnBold.Child = new Image()
            {
                Width = 16,
                Height = 16,
                Source = new BitmapImage(new Uri("pack://application:,,,/LunarMarkdownEditor;component/Images/format-text-bold-5.png")),
            };
            btnBold.MouseLeftButtonDown += BtnBold_MouseLeftButtonDown;
            baseGrid.Children.Add(btnBold);
            Grid.SetColumn(btnBold, 3);

            Border btnItalic = new Border()
            {
                Style = btnStyle,
                Margin = new System.Windows.Thickness(1),
                Width = 24,
                Height = 24,
                BorderBrush = Brushes.Black,
                BorderThickness = new System.Windows.Thickness(1),
                ToolTip = "Ctrl+I，切换倾斜，不支持跨行",
            };
            btnItalic.Child = new Image()
            {
                Width = 16,
                Height = 16,
                Source = new BitmapImage(new Uri("pack://application:,,,/LunarMarkdownEditor;component/Images/format-text-italic-5.png")),
            };
            btnItalic.MouseLeftButtonDown += BtnItalic_MouseLeftButtonDown;
            baseGrid.Children.Add(btnItalic);
            Grid.SetColumn(btnItalic, 4);

            Border btnCodeSpinnet = new Border()
            {
                Style = btnStyle,
                Margin = new System.Windows.Thickness(1),
                Width = 24,
                Height = 24,
                BorderBrush = Brushes.Black,
                BorderThickness = new System.Windows.Thickness(1),
                ToolTip = "Ctrl+`，切换代码片段标记（一对反引号），不支持跨行",
            };
            btnCodeSpinnet.Child = new Image()
            {
                Width = 16,
                Height = 16,
                Source = new BitmapImage(new Uri("pack://application:,,,/LunarMarkdownEditor;component/Images/codespinnet.png")),
            };
            btnCodeSpinnet.MouseLeftButtonDown += BtnCodeSpinnet_MouseLeftButtonDown; ;
            baseGrid.Children.Add(btnCodeSpinnet);
            Grid.SetRow(btnCodeSpinnet, 1);
            Grid.SetColumn(btnCodeSpinnet, 0);

            Border btnUnorderedList = new Border()
            {
                Style = btnStyle,
                Margin = new System.Windows.Thickness(1),
                Width = 24,
                Height = 24,
                BorderBrush = Brushes.Black,
                BorderThickness = new System.Windows.Thickness(1),
                ToolTip = "选定行添加无序列表标记，支持跨行",
            };
            btnUnorderedList.Child = new Image()
            {
                Width = 16,
                Height = 16,
                Source = new BitmapImage(new Uri("pack://application:,,,/LunarMarkdownEditor;component/Images/format-list-unordered.png")),
            };
            btnUnorderedList.MouseLeftButtonDown += BtnUnorderedList_MouseLeftButtonDown;
            baseGrid.Children.Add(btnUnorderedList);
            Grid.SetRow(btnUnorderedList, 1);
            Grid.SetColumn(btnUnorderedList, 1);

            Border btnAddBlockQuoteMark = new Border()
            {
                Style = btnStyle,
                Margin = new System.Windows.Thickness(1),
                Width = 24,
                Height = 24,
                BorderBrush = Brushes.Black,
                BorderThickness = new System.Windows.Thickness(1),
                ToolTip = "选定行添加引用块标记，支持跨行",
            };
            btnAddBlockQuoteMark.Child = new Image()
            {
                Width = 16,
                Height = 16,
                Source = new BitmapImage(new Uri("pack://application:,,,/LunarMarkdownEditor;component/Images/AddBlockQuoterMark.png")),
            };
            btnAddBlockQuoteMark.MouseLeftButtonDown += BtnAddBlockQuoteMark_MouseLeftButtonDown;
            baseGrid.Children.Add(btnAddBlockQuoteMark);
            Grid.SetRow(btnAddBlockQuoteMark, 1);
            Grid.SetColumn(btnAddBlockQuoteMark, 2);

            Border btnAddRegionMark = new Border()
            {
                Style = btnStyle,
                Margin = new System.Windows.Thickness(1),
                Width = 24,
                Height = 24,
                BorderBrush = Brushes.Black,
                BorderThickness = new System.Windows.Thickness(1),
                ToolTip = "选定行添加 region {...} 标记，支持跨行",
            };
            btnAddRegionMark.Child = new Image()
            {
                Width = 16,
                Height = 16,
                Source = new BitmapImage(new Uri("pack://application:,,,/LunarMarkdownEditor;component/Images/brackets.png")),
            };
            btnAddRegionMark.MouseLeftButtonDown += BtnAddRegionMark_MouseLeftButtonDown;
            baseGrid.Children.Add(btnAddRegionMark);
            Grid.SetRow(btnAddRegionMark, 1);
            Grid.SetColumn(btnAddRegionMark, 3);

            Border btnPreviewSegment = new Border()
            {
                Style = btnStyle,
                Margin = new System.Windows.Thickness(1),
                Width = 24,
                Height = 24,
                BorderBrush = Brushes.Black,
                BorderThickness = new System.Windows.Thickness(1),
                ToolTip = "预览选定行编译为Html的效果",
            };
            btnPreviewSegment.Child = new Image()
            {
                Width = 16,
                Height = 16,
                Source = new BitmapImage(new Uri("pack://application:,,,/LunarMarkdownEditor;component/Images/preview-segment.png")),
            };
            btnPreviewSegment.MouseLeftButtonDown += BtnPreviewSegment_MouseLeftButtonDown; ;
            baseGrid.Children.Add(btnPreviewSegment);
            Grid.SetRow(btnPreviewSegment, 1);
            Grid.SetColumn(btnPreviewSegment, 4);

            Border btnBaidu = new Border()
            {
                Style = btnStyle,
                Margin = new System.Windows.Thickness(1),
                Width = 24,
                Height = 24,
                BorderBrush = Brushes.Black,
                BorderThickness = new System.Windows.Thickness(1),
                ToolTip = "调用百度搜索选定的文本",
            };
            btnBaidu.Child = new Image()
            {
                Width = 16,
                Height = 16,
                Source = new BitmapImage(new Uri("pack://application:,,,/LunarMarkdownEditor;component/Images/baidu.png")),
            };
            btnBaidu.MouseLeftButtonDown += BtnBaidu_MouseLeftButtonDown; ; ;
            baseGrid.Children.Add(btnBaidu);
            Grid.SetRow(btnBaidu, 1);
            Grid.SetColumn(btnBaidu, 5);

            Border btnYouDao = new Border()
            {
                Style = btnStyle,
                Margin = new System.Windows.Thickness(1),
                Width = 24,
                Height = 24,
                BorderBrush = Brushes.Black,
                BorderThickness = new System.Windows.Thickness(1),
                ToolTip = "调用有道（YouDao.com）搜索选定的单词",
            };
            btnYouDao.Child = new Image()
            {
                Width = 16,
                Height = 16,
                Source = new BitmapImage(new Uri("pack://application:,,,/LunarMarkdownEditor;component/Images/YouDao.png")),
            };
            btnYouDao.MouseLeftButtonDown += BtnYouDao_MouseLeftButtonDown;
            baseGrid.Children.Add(btnYouDao);
            Grid.SetRow(btnYouDao, 0);
            Grid.SetColumn(btnYouDao, 5);

            //TODO：添加其它快捷工具条按钮


            vPanel.Children.Add(baseGrid);
            this.Child = this.mainBorder = new Border()
            {
                Child = vPanel,
                BorderThickness = new System.Windows.Thickness(1),
                SnapsToDevicePixels = true,
                BorderBrush = Brushes.SaddleBrown,
            };

            this.MouseEnter += EditorPopupToolBar_MouseEnter;
        }

        private void BtnYouDao_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            var activeEdit = Globals.MainWindow.ActivedEditor;
            if (activeEdit == null) return;

            var selectedText = activeEdit.EditorBase.SelectedText;
            if (string.IsNullOrEmpty(selectedText))
            {
                LMessageBox.Show("请先在编辑区选中要搜索的单词！", Globals.AppName, System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Warning);
                return;
            }

            //调用百度搜索在选择区选中的文本
            var sw = Globals.MainWindow.SearchWindow;
            if (sw.Visibility != System.Windows.Visibility.Visible) sw.Show();
            if (sw.WindowState == System.Windows.WindowState.Minimized) sw.WindowState = System.Windows.WindowState.Normal;
            if (sw.IsActive == false) sw.Activate();

            sw.webBrowser.Source = new Uri(string.Format("http://www.youdao.com/w/eng/{0}/#keyfrom=dict2.index", selectedText));
        }

        private void BtnBaidu_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            var activeEdit = Globals.MainWindow.ActivedEditor;
            if (activeEdit == null) return;

            var selectedText = activeEdit.EditorBase.SelectedText;
            if (string.IsNullOrEmpty(selectedText))
            {
                LMessageBox.Show("请先在编辑区选中要搜索的文本！", Globals.AppName, System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Warning);
                return;
            }

            //调用百度搜索在选择区选中的文本
            var sw = Globals.MainWindow.SearchWindow;
            if (sw.Visibility != System.Windows.Visibility.Visible) sw.Show();
            if (sw.WindowState == System.Windows.WindowState.Minimized) sw.WindowState = System.Windows.WindowState.Normal;
            if (sw.IsActive == false) sw.Activate();

            sw.webBrowser.Source = new Uri(string.Format("https://www.baidu.com/s?wd={0}", selectedText));
        }

        private void BtnPaster_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (masterEditorBase != null) masterEditorBase.Paste();
        }

        private void BtnCopy_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (masterEditorBase != null) masterEditorBase.Copy();
        }

        private void BtnCut_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (masterEditorBase != null) masterEditorBase.Cut();
        }

        private void BtnAddRegionMark_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            Globals.MainWindow.WrapWithRegionMark();
        }

        private void BtnPreviewSegment_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            Globals.MainWindow.PreviewMarkdownSegment();
        }

        private Border mainBorder;

        public Border MainBorder { get { return this.mainBorder; } }

        private void EditorPopupToolBar_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            this.MainBorder.Opacity = 1;
            this.entered = true;
        }

        private void BtnCodeSpinnet_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (this.masterEditorBase == null || this.masterEditorBase.SelectionLength == 0) return;

            this.masterEditorBase.SwitchCodeSnippet();
        }

        private void BtnAddBlockQuoteMark_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (this.masterEditorBase == null || this.masterEditorBase.SelectionLength == 0) return;

            this.masterEditorBase.AddBlockQuoterMarksToSelectedLines();
        }

        private void BtnUnorderedList_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (this.masterEditorBase == null || this.masterEditorBase.SelectionLength == 0) return;

            this.masterEditorBase.SwitchListMark(true);
        }

        private void BtnItalic_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (this.masterEditorBase == null || this.masterEditorBase.SelectionLength == 0) return;

            this.masterEditorBase.SwitchItalic();
        }

        private void BtnBold_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (this.masterEditorBase == null || this.masterEditorBase.SelectionLength == 0) return;

            this.masterEditorBase.SwitchBold();
        }

        private bool entered = false;
        /// <summary>
        /// 鼠标指针是否已进入。
        /// </summary>
        public bool Entered
        {
            get { return this.entered; }
            set { this.entered = value; }
        }

        private StackPanel vPanel = new StackPanel() { Orientation = Orientation.Vertical, Background = Brushes.White, };

        public StackPanel VPanel { get { return vPanel; } }


        private MarkdownEditorBase masterEditorBase;
    }
}
